using System;
using System.Configuration;


namespace BIUtilities.Publish.CustomConfiguration
{
    /// <summary>
    /// Used for the children of the deployment configuration element
    /// </summary>
    public class DeploymentElementCollection :
        System.Configuration.ConfigurationElementCollection, IUpdatableConfig
    {
        /// <summary>
        /// Creates a new type safe deployment element
        /// </summary>
        /// <returns></returns>
        protected override System.Configuration.ConfigurationElement CreateNewElement()
        {
            return new DeploymentElement();
        }

        /// <summary>
        /// Gets a specific element's key (type safe)
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(System.Configuration.ConfigurationElement element)
        {
            return ((DeploymentElement)element).Type;
        }

        /// <summary>
        /// Returns the type of collection
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        /// <summary>
        /// The collection, if not 'add' needs to specify its children.
        /// </summary>
        protected override string ElementName
        {
            get
            {
                return "deployment";
            }
        }

        /// <summary>
        /// Method to determine if the element is a child of this
        /// </summary>
        /// <param name="elementName">Name of element found</param>
        /// <returns></returns>
        protected override bool IsElementName(string elementName)
        {
            if ((string.IsNullOrEmpty(elementName)) || (elementName != "deployment"))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Returns a given element child
        /// </summary>
        /// <param name="index">Item index to return</param>
        /// <returns></returns>
        public DeploymentElement this[int index]
        {
            get { return (DeploymentElement)this.BaseGet(index); }
            set
            {
                if (this.BaseGet(index) != null)
                {
                    this.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Makes this element editable
        /// </summary>
        /// <returns></returns>
        public override bool IsReadOnly()
        {
            return false;
        }


        #region IUpdatableConfig Members
        /// <summary>
        /// Creating a public accessor to get at the property values
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        object IUpdatableConfig.this[string propertyName]
        {
            get { return base[propertyName]; }
            set { base[propertyName] = value; }
        }
        #endregion

    }
}
